package com.system.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.page.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.system.module.admin.request.AttendanceRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.system.module.admin.mapper.AttendanceMapper;
import org.springframework.beans.BeanUtils;
import com.system.module.admin.entity.Attendance;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.system.module.admin.service.IAttendanceService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 打卡记录业务层处理
 *
 * @author mars
 * @date 2024-04-13
 */
@Slf4j
@Service
@AllArgsConstructor
public class AttendanceServiceImpl implements IAttendanceService {

    private final AttendanceMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Attendance add(AttendanceRequest request) {
        Attendance entity = Attendance.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AttendanceRequest request) {
        Attendance entity = Attendance.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public Attendance getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<Attendance> pageList(AttendanceRequest request) {
        Page<Attendance> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<Attendance> query = this.buildWrapper(request);
        IPage<Attendance> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<Attendance> list(AttendanceRequest request) {
        LambdaQueryWrapper<Attendance> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<Attendance> buildWrapper(AttendanceRequest param) {
        LambdaQueryWrapper<Attendance> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getSubmitTime())){
               query.eq(Attendance::getSubmitTime ,param.getSubmitTime());
        }
        if (param.getUserId() != null){
               query.eq(Attendance::getUserId ,param.getUserId());
        }
         if (StringUtils.isNotBlank(param.getUsername())){
               query.like(Attendance::getUsername ,param.getUsername());
        }
        if (param.getSubjectId() != null){
               query.eq(Attendance::getSubjectId ,param.getSubjectId());
        }
         if (StringUtils.isNotBlank(param.getSubjectName())){
               query.like(Attendance::getSubjectName ,param.getSubjectName());
        }
        if (StringUtils.isNotBlank(param.getEndTime())){
               query.eq(Attendance::getEndTime ,param.getEndTime());
        }
        return query;
    }

}
